package com.jose.photogallery;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.jose.photogallery.domain.Photo;
import com.jose.photogallery.domain.PhotogalleryDAO;
import com.jose.photogallery.domain.PhotogalleryUtil;

@SuppressWarnings("serial")
public class RatingServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		PhotogalleryDAO dao = new PhotogalleryDAO();
		
		if (user != null){
			String logoutUrl = userService.createLogoutURL("/");
			req.setAttribute("logoutUrl", logoutUrl);
			req.setAttribute("user", user);
			
			// Get request parameters
			String key = (String) req.getParameter("key");
			int rating = Integer.parseInt(req.getParameter("vote"));
			
			// Response JSON object
			JSONObject json = new JSONObject();
			
			if( PhotogalleryUtil.hasVoted(key, user.getUserId())){
				//req.setAttribute("message", "Sorry, You already voted for this Photo!");
				//resp.getWriter().println("Sorry, you already voted for this photo!");
				try {
					json.put("success", false);
					json.put("msg",	"Sorry, you already voted for this Photo");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				// Add the voting to the photo
				PhotogalleryUtil.setRating(rating, key, user.getUserId());
				Photo p = dao.getPhoto(key);
				
				try {
					json.put("success", true);
					json.put("msg",	"Thanks for voting!");
					json.put("avg", p.getRating());
					json.put("votes", p.getVotes());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			resp.setContentType("application/json");
			resp.getWriter().println(json);
			//req.getRequestDispatcher("/WEB-INF/JSP/messagepage.jsp").forward(req, resp);
		} else {
			String loginUrl = userService.createLoginURL("/");
			req.setAttribute("loginUrl", loginUrl);
			req.setAttribute("error", "Sorry, you need to login to access this page!");
			req.getRequestDispatcher("/WEB-INF/JSP/errorpage.jsp").forward(req, resp);
		}
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException, ServletException  {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		// Set proper parameters if the user is log in
		if (user != null){
			String logoutUrl = userService.createLogoutURL("/");
			req.setAttribute("logoutUrl", logoutUrl);
			req.setAttribute("user", user);
		} else {
			String loginUrl = userService.createLoginURL("/");
			req.setAttribute("loginUrl", loginUrl);
		}

		req.setAttribute("error", "Sorry, you dont have acces to this site");
		req.getRequestDispatcher("/WEB-INF/JSP/errorpage.jsp").forward(req, resp);
	}
}
